package GUI;

import javax.swing.JPanel;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.JLabel;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;

import java.awt.Color;

import javax.swing.Box;
import javax.swing.table.TableRowSorter;
import javax.swing.RowFilter;

import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.UIManager;

import ModelLayer.CustomerContainer;
import ModelLayer.Item;
import ModelLayer.PartSale;
import ModelLayer.Sale;
import ModelLayer.SaleContainer;
import net.miginfocom.swing.MigLayout;


import ControlLayer.*;
import ModelLayer.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class NewSalePanel extends JPanel {
	private JTextField newSale_ItemSearch_txt;
	private JTable newSale_Item_tabel;
	private JTextField newSale_findCustomer_Txt;
	private JTable newSale_Customer_Tabel;
	private JTable newSale_SaleInfo_table;
	private JTextField newSale_ItemAmount_txt;
	
	private JLabel newSale_SelectedCustomer_Label;
	private JLabel newSale_CurrentDiscount_Label;
	private JLabel newSale_CurrentTotalPrice_Label;
	private JLabel newSale_CurrentEmployee_Label;
	
	// for table
	private JTable customerTable;
	private ArrayList<Customer> customerList;
	private OurCustomTableModel customerTableModel;
	
	private JTable itemTable;
	private ArrayList<Item> itemList;
	private OurCustomTableModel itemTableModel;
		
	private JTable partSaleTable;
	private ArrayList<PartSale> partSaleList;
	private OurCustomTableModel partSaleTableModel;
	
	// controller
	private SaleCtr saleCtr;
	private PersonCtr personCtr;
	private Sale newSale;
	private String Employee;
	
	private NewSaleModel newSaleModel;
	
	public NewSalePanel() {
		// setting up newSale
		saleCtr = new SaleCtr();
		personCtr = new PersonCtr();
		
		Employee newSaleEmployee = new Employee("Test ansat", "vej", "telefon", "email", "ID");
		//(EmployeeContainer.getInstance().addEmployee
		//saleCtr.addEmployee("ID"); 
		newSale = new Sale();
		
		// test employee
		
		
		// setting up panel
		setLayout(new MigLayout("", "[311.00,grow][433.00]", "[grow]"));
		
		JPanel newSale_add_panel = new JPanel();
		newSale_add_panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Tilf\u00F8j varer og kunde til salget", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		add(newSale_add_panel, "cell 0 0,grow");
		newSale_add_panel.setLayout(new MigLayout("", "[grow]", "[282.00,grow][281.00,grow]"));
		
		JPanel newSale_FindAddItem_panel = new JPanel();
		newSale_FindAddItem_panel.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		newSale_add_panel.add(newSale_FindAddItem_panel, "cell 0 0,grow");
		newSale_FindAddItem_panel.setLayout(new MigLayout("", "[grow][]", "[][][grow][][grow]"));
		
		JLabel newSale_FindItem_label = new JLabel("Find vare");
		newSale_FindAddItem_panel.add(newSale_FindItem_label, "cell 0 0");
		
		newSale_ItemSearch_txt = new JTextField();
		newSale_ItemSearch_txt.setToolTipText("S\u00F8g efter et vare ID.");
		newSale_FindAddItem_panel.add(newSale_ItemSearch_txt, "cell 0 1,growx");
		newSale_ItemSearch_txt.setColumns(10);
		
		//---------------------START OF NEW SALE ITEM CONTENT-------------------------------------------------------
		// setting up the table information
		// column names for the table

		
		String[] itemColumnNames = {"Varekode", "Navn", "Type", "Pris", "Rabat"};
		
		itemList = StorageContainer.getInstance().getItemsArrayList();
		// newsale 
		// add newsale
		
		try {
			itemTableModel = new OurCustomTableModel("ModelLayer.Item", itemColumnNames, itemList);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			e1.printStackTrace();
		}
					

		itemTable = new JTable(itemTableModel);			
		final TableRowSorter<TableModel> itemSorter = new TableRowSorter<TableModel>(itemTableModel);
		itemTable.setRowSorter(itemSorter);			
		itemTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		itemTable.setFillsViewportHeight(true);
							
	
				
						
		
		
		
		itemTable.setBorder(new LineBorder(new Color(0, 0, 0)));
		newSale_FindAddItem_panel.add(new JScrollPane(itemTable), "cell 0 2 2 1,grow");
		
		JButton newSale_ItemAdd_button = new JButton("Tilf\u00F8j");
		newSale_ItemAdd_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addNewItemtoNewSale();
			}
		});
		
		JLabel lblAntal = new JLabel("Antal");
		newSale_FindAddItem_panel.add(lblAntal, "cell 0 3,alignx left");
		
		newSale_ItemAmount_txt = new JTextField();
		newSale_FindAddItem_panel.add(newSale_ItemAmount_txt, "cell 0 4,growx");
		newSale_ItemAmount_txt.setColumns(10);
		newSale_FindAddItem_panel.add(newSale_ItemAdd_button, "cell 1 4");
		
		JPanel newSale_FindAddCustomer_panel = new JPanel();
		newSale_FindAddCustomer_panel.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		newSale_add_panel.add(newSale_FindAddCustomer_panel, "cell 0 1,grow");
		newSale_FindAddCustomer_panel.setLayout(new MigLayout("", "[grow][54.00][]", "[][][grow][]"));
		
		JLabel newSale_FindCustomer_label = new JLabel("Find kunde");
		newSale_FindAddCustomer_panel.add(newSale_FindCustomer_label, "cell 0 0");
		
		newSale_findCustomer_Txt = new JTextField();
		newSale_findCustomer_Txt.setToolTipText("S\u00F8g efter et kunde ID");
		newSale_FindAddCustomer_panel.add(newSale_findCustomer_Txt, "cell 0 1,growx");
		newSale_findCustomer_Txt.setColumns(10);
		
		//---------------------START OF NEW SALE CUSTOMERTABLE CONTENT-------------------------------------------------------
		// setting up the table information
		// column names for the table



		String[] columnNames = {"Navn", "Adresse", "Telefon nummer", "E-mail", "Kunde ID", "Kunde Rabat", "CVR Nummer"};
		
		customerList = CustomerContainer.getInstance().getCustomersArrayList();
		// newsale 
		// add newsale
		
		try {
			customerTableModel = new OurCustomTableModel("ModelLayer.Customer", columnNames, customerList);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			e1.printStackTrace();
		}
					
		//final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
		//setting up the actual table
		customerTable = new JTable(customerTableModel);			
		
		// setting up sorter
		//sorter = new TableRowSorter<OurCustomTableModel>(itemTableModel);
		//itemTable.setRowSorter(sorter);
		
		// setting up columns 
		customerTable.setTableHeader(new JTableHeader());		
		customerTable.getTableHeader().setReorderingAllowed(false);
		
		// model
		customerTable = new JTable(customerTableModel);
		final TableRowSorter<TableModel> customerSorter = new TableRowSorter<TableModel>(customerTableModel);
		customerTable.setRowSorter(customerSorter);
		customerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		customerTable.setFillsViewportHeight(true);
		

		//---------------------END OF NEW SALE CUSTOMER TABLE CONTENT-----------------------------------------------------
		

		customerTable.setBorder(new LineBorder(new Color(0, 0, 0)));
		newSale_FindAddCustomer_panel.add(new JScrollPane(customerTable), "cell 0 2 3 1,grow");
		
		JButton newSale_CustomerAdd_Button = new JButton("Tilf\u00F8j");
		newSale_CustomerAdd_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addCustomerToNewSale();
			}
		});
		newSale_FindAddCustomer_panel.add(newSale_CustomerAdd_Button, "cell 2 3");
		
		JPanel newSale_SaleInfo_Panel = new JPanel();
		newSale_SaleInfo_Panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Salgs information", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		add(newSale_SaleInfo_Panel, "cell 1 0,grow");
		newSale_SaleInfo_Panel.setLayout(new MigLayout("", "[grow]", "[grow]"));
		
		JPanel newSale_CurrentSaleInfo_Panel = new JPanel();
		newSale_CurrentSaleInfo_Panel.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		newSale_SaleInfo_Panel.add(newSale_CurrentSaleInfo_Panel, "cell 0 0,grow");
		newSale_CurrentSaleInfo_Panel.setLayout(new MigLayout("", "[124.00][62.00,grow][][][]", "[][][][280.00,grow][][][][]"));
		
		JLabel newSale_Employee_Label = new JLabel("Medarbejder");
		newSale_CurrentSaleInfo_Panel.add(newSale_Employee_Label, "cell 0 0");
		
		newSale_CurrentEmployee_Label = new JLabel("<TestAnsat>");
		newSale_CurrentSaleInfo_Panel.add(newSale_CurrentEmployee_Label, "cell 1 0");
		
		JLabel newSale_Customer_Label = new JLabel("Kunde");
		newSale_CurrentSaleInfo_Panel.add(newSale_Customer_Label, "cell 0 1");
		
		newSale_SelectedCustomer_Label = new JLabel("");
		newSale_CurrentSaleInfo_Panel.add(newSale_SelectedCustomer_Label, "cell 1 1");
		
		JLabel newSale_InfoSale_Label = new JLabel("Tilf\u00F8jede vare i salget");
		newSale_CurrentSaleInfo_Panel.add(newSale_InfoSale_Label, "cell 0 2");
		
				
		//------------------New Sale Table------------------------
		String[] newSalecolumnNames = {"Varekode", "Navn", "Antal", "Pris", "Total prisen"};
		partSaleList = Sale.getInstance().getPartSaleArrayList();
		
		newSaleModel = new NewSaleModel(newSalecolumnNames, partSaleList);

		//--------------End of New Sale Table---------------------

		
		newSale_SaleInfo_table = new JTable();
		newSale_SaleInfo_table.setModel(newSaleModel);
		// setting up columns 
		newSale_SaleInfo_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		newSale_SaleInfo_table.setFillsViewportHeight(true);
		
		
		
		newSale_SaleInfo_table.setBorder(new LineBorder(new Color(0, 0, 0)));
		newSale_CurrentSaleInfo_Panel.add(new JScrollPane(newSale_SaleInfo_table), "cell 0 3 5 1,grow");
		
		JLabel newSale_TotalPrice_Label = new JLabel("Total pris (uden kunde rabat)");
		newSale_CurrentSaleInfo_Panel.add(newSale_TotalPrice_Label, "cell 0 4");
		
		newSale_CurrentTotalPrice_Label = new JLabel("0.0");
		newSale_CurrentSaleInfo_Panel.add(newSale_CurrentTotalPrice_Label, "cell 1 4,alignx right");
		
		JLabel label = new JLabel("DKR");
		newSale_CurrentSaleInfo_Panel.add(label, "cell 2 4");
		
		JLabel newSale_Discount_Label = new JLabel("Rabat");
		newSale_CurrentSaleInfo_Panel.add(newSale_Discount_Label, "cell 0 5");
		
		newSale_CurrentDiscount_Label = new JLabel("0.0");
		newSale_CurrentSaleInfo_Panel.add(newSale_CurrentDiscount_Label, "cell 1 5,alignx right");
		
		JButton newSale_Cancel_button = new JButton("Annullere K\u00F8bet");
		newSale_Cancel_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearNewSale();
			}
		});
		
		JLabel label_1 = new JLabel("%");
		newSale_CurrentSaleInfo_Panel.add(label_1, "cell 2 5");
		newSale_CurrentSaleInfo_Panel.add(newSale_Cancel_button, "cell 3 7,alignx right");
		
		JButton newSale_Payment_Button = new JButton("Betaling");
		newSale_Payment_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				payForSale();
			}
		});
		newSale_CurrentSaleInfo_Panel.add(newSale_Payment_Button, "cell 4 7,alignx right");
		
		JButton newSale_ItemSearch_button = new JButton("S\u00F8g");
		newSale_ItemSearch_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String itemText = newSale_ItemSearch_txt.getText();
					if(itemText.length() == 0) {
						itemSorter.setRowFilter(null);
					} else {
						itemSorter.setRowFilter(RowFilter.regexFilter(itemText));
					}
			}
		});
		newSale_FindAddItem_panel.add(newSale_ItemSearch_button, "cell 1 1,alignx right");
		
		JButton newSale_CustomerSearch_button = new JButton("S\u00F8g");
		newSale_CustomerSearch_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String customerText = newSale_findCustomer_Txt.getText();
					if(customerText.length() == 0) {
						customerSorter.setRowFilter(null);
					} else {
						customerSorter.setRowFilter(RowFilter.regexFilter(customerText));
					}
			}
		});
		newSale_FindAddCustomer_panel.add(newSale_CustomerSearch_button, "cell 2 1");
	}

	public void addNewItemtoNewSale() {
		int amount = 0;
		
		int index = itemTable.getSelectedRow();
		Item newSaleItem = itemList.get(index);
		try {
			if(!newSale_ItemAmount_txt.getText().isEmpty()) {
				amount = Integer.valueOf(newSale_ItemAmount_txt.getText());
			}			
		}		
		catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		if (newSaleItem != null && amount > 0) {
			try {
				saleCtr.addItem(newSaleItem.getItemCode(), amount);
				newSale_SaleInfo_table.repaint();

			}
			catch (NullPointerException e) {
				e.printStackTrace();
			}
			finally {
				
				newSale_CurrentTotalPrice_Label.setText(String.valueOf(saleCtr.getNewSale().getTotalPrice()));
			}
		}	
	}
	
	public void addCustomerToNewSale() {
		int index = customerTable.getSelectedRow();
		Customer saleCustomer = customerList.get(index);
		
		if(saleCustomer != null) {
			String customerID = saleCustomer.getCustomerID();
			saleCtr.addCustomer(customerID);
			
			String saleCustomerDiscount = String.valueOf(saleCustomer.getDiscount() * 100) ;
			
			// sets name label for customer
			newSale_SelectedCustomer_Label.setText(saleCustomer.getName());
			
			// sets discount label for customer
			newSale_CurrentDiscount_Label.setText(saleCustomerDiscount);
		}		
	}
	
	
	public void payForSale() {
		double currentPriceTotal = Double.valueOf(newSale_CurrentTotalPrice_Label.getText());
		if(!(currentPriceTotal <= 0) && saleCtr.getNewSale().getEmployee() != null) {
			try {
				double neededPaymentAmount = Double.valueOf(saleCtr.getNewSale().getTotalPrice() * (1 - saleCtr.getNewSale().getCustomer().getDiscount()));
				PaymentWindow paymentWindow = new PaymentWindow(neededPaymentAmount);
				paymentWindow.setVisible(true);
			} 
			finally {
				
				
				clearForNewSale();
				
				
			}
		}
	}
	
	public void clearNewSale() {
		
			try {
				
				clearForNewSale();
			}
			catch (NullPointerException e) {
				e.printStackTrace();
			}
		
	}
	
	public void clearForNewSale() {
		
		try {
			partSaleList.clear();
			newSaleModel.fireTableDataChanged();
			saleCtr.getNewSale().setTotalPrice(0.0);
			
			
			saleCtr.addEmployee("ID");
			newSale_SelectedCustomer_Label.setText("");
			newSale_CurrentTotalPrice_Label.setText("0.0");
			newSale_CurrentDiscount_Label.setText("0.0");
		}
		catch (NullPointerException e) {
			e.printStackTrace();
		}
	}
	
}
	
	